package com.hsnn.medstgmini.pro.base.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.base.std.model.HospInfo;
import com.hsnn.medstgmini.pro.base.service.AnaHospManager;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.util.DbUtil;
import com.hsnn.medstgmini.util.Pagination;
import com.hsnn.medstgmini.util.ParseDate;

/**
 * @category 医院端统计分析
 * @author lil
 * @date 2015年12月7日
 */
@Controller
@RequestMapping("/anaHosp")
public class AnaHospfoController {

	private static final Logger log = Logger.getLogger(AnaHospfoController.class);
	@Autowired
	private  AnaHospManager anaHospManager;
	
	@Autowired
	private HttpServletRequest request;

	@Autowired
	private HttpSession session;
	
	/**
	 * @category 按时间统计
	 * @date 2015年12月7日
	 * @author lil
	 * @return
	 */
	@RequestMapping("/toAnaByTime")
	public String toAnaByTime() {
		return "/pro/base/anaHosp/anaByTime";
	}
	@ResponseBody
	@RequestMapping("/getAnaByTime")
	public Pagination getAnaByTime() {
		Pagination page = new Pagination(request);
		try {
			DbUtil.jsonToMap(request, page);
			SysUser user = (SysUser) request.getSession().getAttribute(Constants.USERINFO);
			HospInfo hi = (HospInfo)user.getInfo();
			page.getConditions().put("hospCode", hi.getHospCode());
			String starttime = request.getParameter("starttime");
			if (StringUtils.isNotBlank(starttime)){
				starttime = ParseDate.parseYMDFormat(ParseDate.fromShortFormat(starttime));
				page.getConditions().put("starttime", starttime);
			}
			
			String endtime = request.getParameter("endtime");
			if (StringUtils.isNotBlank(endtime)){
				endtime = ParseDate.parseYMDFormat(ParseDate.fromShortFormat(endtime));
				page.getConditions().put("endtime", endtime);
			}
			
			page = anaHospManager.getAnaListByTime(page);
			page.setSuccess(true);
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}
	
	
	
	/**
	 * @category 按时间统计药品采购月汇总信息
	 * @date 2015年12月07日
	 * @return
	 */
	@RequestMapping("/toAnaByTimeMonth")
	public String toAnaByTimeMonth() {
		return "/pro/base/anaHosp/anaByTimeMonth";
	}
	@ResponseBody
	@RequestMapping("/getAnaByTimeMonth")
	public Pagination getAnaByTimeMonth() {
		Pagination page = new Pagination(request);
		try {
			DbUtil.jsonToMap(request, page);
			SysUser user = (SysUser) request.getSession().getAttribute(Constants.USERINFO);
			HospInfo hi = (HospInfo)user.getInfo();
			page.getConditions().put("hospCode", hi.getHospCode());
			String starttime = request.getParameter("starttime");
			if (StringUtils.isNotBlank(starttime)){
				starttime = ParseDate.parseShortYearFormat(ParseDate.fromYearMonthFormat(starttime)) + ParseDate.parseShortMonthFormat(ParseDate.fromYearMonthFormat(starttime));
				page.getConditions().put("starttime", starttime);
			}
			
			String endtime = request.getParameter("endtime");
			if (StringUtils.isNotBlank(endtime)){
				endtime = ParseDate.parseShortYearFormat(ParseDate.fromYearMonthFormat(endtime)) + ParseDate.parseShortMonthFormat(ParseDate.fromYearMonthFormat(endtime));
				page.getConditions().put("endtime", endtime);
			}
			
			page = anaHospManager.getAnaListByTimeMonth(page);
			page.setSuccess(true);
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}
	
	/**
	 * @category 按时间统计药品采购季度汇总信息
	 * @date 2015年12月07日
	 * @return
	 */
	@RequestMapping("/toAnaByTimeQuarter")
	public String toAnaByTimeQuarter() {
		return "/pro/base/anaHosp/anaByTimeQuarter";
	}
	@ResponseBody
	@RequestMapping("/getAnaByTimeQuarter")
	public Pagination getAnaByTimeQuarter() {
		Pagination page = new Pagination(request);
		try {
			SysUser user = (SysUser) request.getSession().getAttribute(Constants.USERINFO);
			HospInfo hi = (HospInfo)user.getInfo();
			page.getConditions().put("hospCode", hi.getHospCode());
			DbUtil.jsonToMap(request, page);
			page = anaHospManager.getAnaListByTimeQuarter(page);
			page.setSuccess(true);
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}
	
	/**
	 * @category 按时间统计药品采购年汇总信息
	 * @date 2015年12月07日
	 * @return
	 */
	@RequestMapping("/toAnaByTimeYear")
	public String toAnaByTimeYear() {
		return "/pro/base/anaHosp/anaByTimeYear";
	}
	@ResponseBody
	@RequestMapping("/getAnaByTimeYear")
	public Pagination getAnaByTimeYear() {
		Pagination page = new Pagination(request);
		try {
			SysUser user = (SysUser) request.getSession().getAttribute(Constants.USERINFO);
			HospInfo hi = (HospInfo)user.getInfo();
			page.getConditions().put("hospCode", hi.getHospCode());
			DbUtil.jsonToMap(request, page);
			page = anaHospManager.getAnaListByTimeYear(page);
			page.setSuccess(true);
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}
}
